package com.example.smartcity.ui.dashboard;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.PassengerMesListViewAdapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class SmartBarSetSelftMessage extends AppCompatActivity {
    SharedPreferences sharedPreferences;
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    private ListView passengerMessageListView;
    private Button nextTwo;
    String[] array = {"日期选择","时间选择","姓名","手机号码","上车地点","下车地点"};
    ArrayList<String> list = new ArrayList<>();
    ArrayList<String> item;
    String[] newItem;
    String[] newItem2;
    String userName;
    String userPhone;
    String start;
    String end;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_bar_set_selft_message);
        passengerMessageListView = (ListView) findViewById(R.id.passengerMessageListView);
        nextTwo = (Button) findViewById(R.id.nextTwo);
        initView();
        setBaseMessage();
        setNextTwo();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        //设置时间
        Intent intent = getIntent();
        //获取上车地址
        item = intent.getStringArrayListExtra("stop");
        newItem2 = new String[item.size()];

        for (int i = 0; i < item.size(); i++) {
            newItem2[i] = item.get(i);
        }
        item.remove(item.size()-1);
        newItem = new String[item.size()];
        for (int i = 0; i < item.size(); i++) {
            newItem[i] = item.get(i);
        }

        DateFormat formatD = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat formatH = new SimpleDateFormat("hh:mm");
        list.add(formatD.format(date));
        list.add(formatH.format(date));
        sharedPreferences = getSharedPreferences("selfMessage",MODE_PRIVATE);
        userName = sharedPreferences.getString("userName",null);
        userPhone = sharedPreferences.getString("userPhone",null);
        //标记
        list.add(userName);
        list.add(userPhone);
        start = intent.getStringExtra("start");
        end = intent.getStringExtra("end");
        list.add(start);
        list.add(end);
        PassengerMesListViewAdapter passengerMesListViewAdapter = new PassengerMesListViewAdapter(SmartBarSetSelftMessage.this,R.layout.passengermesitemlayout,list,array);
        passengerMessageListView.setAdapter(passengerMesListViewAdapter);
    }
    public void setBaseMessage(){
        passengerMessageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                switch (position){
                    case 0:
                        //修改日期
                        new DatePickerDialog(SmartBarSetSelftMessage.this, new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                TextView date = v.findViewById(R.id.TV_SetMessage);
                                date.setText(year+"-"+month+"-"+dayOfMonth);
                            }
                        },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
                        break;
                    case 1:
                        //修改时间对话框
                        new TimePickerDialog(SmartBarSetSelftMessage.this, new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                                TextView date = v.findViewById(R.id.TV_SetMessage);
                                date.setText(hourOfDay+":"+minute);
                            }
                        },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
                        break;
                    case 4:
                        //修改上车地址
                        final String[] temp = {null};
                        Log.d("TAG", "onItemClick: "+newItem.toString());
                        new AlertDialog.Builder(SmartBarSetSelftMessage.this)
                                .setTitle("请选择上车地点")
                                .setSingleChoiceItems(newItem, 0, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        TextView TV_start = v.findViewById(R.id.TV_SetMessage);
                                        TV_start.setText(newItem[which]==TV_start.getText()?newItem[which]:newItem[which]);
                                    }
                                })
                                .setPositiveButton("确定",null)
                                .show();
                        break;
                    case 5:
                        //修改下车地址
                        final String[] Temp = new String[newItem2.length+1];
                        new AlertDialog.Builder(SmartBarSetSelftMessage.this)
                                .setTitle("请选择下车地点")
                                .setSingleChoiceItems(newItem2, newItem2.length-1, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        TextView TV_start = v.findViewById(R.id.TV_SetMessage);
                                        TV_start.setText(newItem2[which]==TV_start.getText()?newItem2[which]:newItem2[which]);
                                    }
                                })
                                .setPositiveButton("确定",null)
                                .show();
                        break;
                }
            }
        });

    }
    public void setNextTwo(){
        nextTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SmartBarSetSelftMessage.this,SubmitOrder.class);
                intent.putExtra("end",end);
                intent.putExtra("start",start);
                intent.putExtra("userPhone",userPhone);
                intent.putExtra("userName",userName);
                startActivity(intent);
            }
        });
    }
}